/**
 * 版权所有：2020，TLF工作室。
 */
package com.cqeec.gams.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cqeec.gams.entity.Account;
import com.cqeec.gams.entity.Role;
import com.cqeec.gams.service.RoleService;

/**
 * @author 唐礼飞
 * @date 2020-06-04 08:40:36.170
 */
@RestController
@RequestMapping("/api/role/")
public class RoleRestController {
	@Autowired
	private RoleService roleService;

	@PostMapping("/change/disabled/{id}/{checked}")
	public Map<String, Object> disabled(@PathVariable("id") String id, @PathVariable("checked") Boolean checked) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			roleService.changeDisabled(id,checked);
			map.put("code", 0);
			map.put("message", "更改角色禁用状态成功！");
		}catch (Exception ex) {
			ex.printStackTrace();
			map.put("code", 1);
			map.put("message", "更改角色禁用状态失败！" + ex.getMessage());
		}
		
		return map;
	}

	@PostMapping("/save")
	public Map<String, Object> save(Role role, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Object obj = session.getAttribute("account");
			if (obj == null) {
				map.put("code", 1);
				map.put("message", "账户未登录，请登录后再来添加数据！");
			} else {
				Account account = (Account) obj;
				role.setCreator(account.getId());
				role.setCreatorName(account.getName());
				roleService.save(role);
				map.put("code", 0);
				map.put("message", "保存数据成功！");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			map.put("code", 2);
			map.put("message", "保存数据失败！" + ex.getMessage());
		}
		return map;
	}
}
